//
//  NetworkTools.m
//  OC网络请求封装
//
//  Created by Apple on 16/9/18.
//  Copyright © 2016年 KY. All rights reserved.
//

#import "HMNetworkTools.h"
#import "HMOAuthViewController.h"
#import "HMGlobalConst.h"


//@interface NetworkTools ()
//@property(nonatomic, copy) void (^callBack) (id a, NSString *s);
//
//@end

//NSString *WeiboAppKey = @"2461615762";
//
//NSString *WeiboAppSecret = @"c02318ad41df10197e807191a51015c4";
//
//NSString *WeiboRedirect_Uri = @"http://www.baidu.com";

@implementation HMNetworkTools

+ (instancetype)sharedTools {
    
    static HMNetworkTools *tools;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        tools = [HMNetworkTools manager];
        //  添加可接受的响应数据类型
        tools.responseSerializer.acceptableContentTypes = [tools.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray:@[@"text/html",@"text/plain"]];
        
    });
    
    return tools;

}


- (void)requestWithType: (ReqeustType)type andUrl: (NSString *)url andParams: (id)params andCallBack: (void (^) (id result, NSError *error))callBack {
    
    
    if (type == GET) {
    
        [self GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            callBack(responseObject, nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            callBack(nil, error);
        }];
        
    } else {
        [self POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            callBack(responseObject, nil);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            callBack(nil, error);
        }];
    }
}





   
    
-(void)requestAccessTokenWithCode:(NSString *)code andCallBack:(void (^)(id, NSError *))callBack{

    //  请求接口地址
    NSString *url = @"https://api.weibo.com/oauth2/access_token";
    //  准备请求需要的参数
    NSDictionary * params = @{
                  @"client_id": WeiboAppKey,
                  @"client_secret": WeiboAppSecret,
                  @"grant_type": @"authorization_code",
                  @"code": code,
                  @"redirect_uri": WeiboRedirect_Uri
                  
                  };
    
    [self requestWithType:POST andUrl:url andParams:params andCallBack:^(id result, NSError *error) {
        callBack(result,error);
    }];
    

}
//获取用户信息
-(void)requestUserInfoWithAccessToken:(NSString *)accessToken uid:(int64_t)uid andCallBack:(void (^)(id, NSError *))callBack{
    
    NSString * url = @"https://api.weibo.com/2/users/show.json";
    //  准备请求需要的参数
    NSDictionary * params = @{
                                 @"access_token": accessToken,
                                 @"uid": @(uid)
                                 };

    [self requestWithType:GET andUrl:url andParams:params andCallBack:^(id result, NSError *error) {
        callBack(result,error);
    }];
}


-(void)requestStatusesWithAccessToken:(NSString *)accessToken maxId:(int64_t)maxId sinceId:(int64_t)sinceId andCallBack:(void (^)(id, NSError *))callBack{
    
    NSString *url = @"https://api.weibo.com/2/statuses/friends_timeline.json";
    NSDictionary * params = @{
                              @"access_token": accessToken,
                              @"max_id": [NSNumber numberWithLongLong:maxId],
                              @"since_id": [NSNumber numberWithLongLong:sinceId]
                              };
    
    [self requestWithType:GET andUrl:url andParams:params andCallBack:^(id result, NSError *error) {
        callBack(result,error);
    }];
    
}

/**
 发文字微博的接口

 @param accessToken 令牌
 @param status      微博内容
 @param callBack    发送结果回调的Block
 */
-(void)updateWithAccess_token:(NSString *)accessToken status:(NSString *)status andCallBack:(void (^)(id, NSError *))callBack{
    NSString *url = @"https://api.weibo.com/2/statuses/update.json";
    NSDictionary * params = @{
                              @"access_token": accessToken,
                              @"status": status
                              };
    [self requestWithType:POST andUrl:url andParams:params andCallBack:callBack];

    
}

-(void)updateWithAccess_token:(NSString *)accessToken status:(NSString *)status image:(UIImage *)image andCallBack:(void (^)(id, NSError *))callBack{
    NSString *url = @"https://upload.api.weibo.com/2/statuses/upload.json";
    NSDictionary * params = @{
                              @"access_token": accessToken,
                              @"status": status
                              };
    
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
    
    [self POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:imageData name:@"pic" fileName:@"test" mimeType:@"image/jpeg"];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        callBack(responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        callBack(nil,error);
    }];

}




@end
